package com.story.modular.app.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.story.common.annotion.BussinessLog;
import com.story.common.annotion.Permission;
import com.story.common.constant.Dict;
import com.story.common.constant.factory.ConstantFactory;
import com.story.common.exception.BizExceptionEnum;
import com.story.common.exception.BussinessException;
import com.story.common.persistence.dao.SocialContactMapper;
import com.story.common.persistence.model.SocialContact;
import com.story.core.base.controller.BaseController;
import com.story.core.log.LogObjectHolder;
import com.story.core.util.RegExpValidatorUtil;
import com.story.core.util.ToolUtil;
import com.story.modular.app.warpper.SocialContactWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @decription 社交账号管理
 * @Date 2017-12-29 14:31
 */
@Controller
@RequestMapping(value = "/social")
public class SocialContactController extends BaseController{

    public static final String PREFIX = "/app/social/";

    @Autowired
    private SocialContactMapper socialContactMapper;

    /**
     * 进入列表页面
     * @return
     */
    @RequestMapping(value = "")
    public String index() {
        return PREFIX + "social.html";
    }

    /**
     * 社交账号列表
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list() {
        List<SocialContact> socialContactList = socialContactMapper.selectList(new EntityWrapper<SocialContact>());
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        for (SocialContact socialContact : socialContactList) {
            Map<String,Object> resultMap = new HashMap<String,Object>();
            resultMap.put("id",socialContact.getId());
            resultMap.put("type",socialContact.getType());
            resultMap.put("account",socialContact.getAccount());
            resultList.add(resultMap);
        }
        return super.warpObject(new SocialContactWarpper(resultList));
    }

    /**
     * 进入添加页面
     * @return
     */
    @RequestMapping(value = "/socialAdd")
    public String toAdd() {
        return PREFIX + "social_add.html";
    }

    /**
     * 新增账号
     * @param socialContact
     * @return
     */
    @RequestMapping(value = "/add")
    @Permission
    @ResponseBody
    public Object add(SocialContact socialContact) {
        if(ToolUtil.isOneEmpty(socialContact,socialContact.getAccount(),socialContact.getType())) {
            throw new BussinessException( BizExceptionEnum.REQUEST_NULL);
        }
        if("qq".equals(socialContact.getType())) {
            if(!RegExpValidatorUtil.isQq(socialContact.getAccount())) {
                return new BussinessException(BizExceptionEnum.QQ_INVALID);
            }
        }
        socialContactMapper.insert(socialContact);
        return super.SUCCESS_TIP;
    }

    /**
     * 进入编辑页面
     * @param socialId
     * @param model
     * @return
     */
    @RequestMapping(value = "/socialUpdate/{socialId}")
    public String toUpdate(@PathVariable(value = "socialId") Integer socialId,Model model) {
        SocialContact socialContact = socialContactMapper.selectById(socialId);
        model.addAttribute(socialContact);
        model.addAttribute("sName", ConstantFactory.me().getSocialNameById(socialId));
        LogObjectHolder.me().set(socialContact);
        return PREFIX + "social_update.html";
    }

    /**
     * 修改账号
     * @param socialContact
     * @return
     */
    @RequestMapping(value = "/update")
    @Permission
    @ResponseBody
    public Object update(SocialContact socialContact) {
        if(ToolUtil.isOneEmpty(socialContact,socialContact.getAccount(),socialContact.getType(),socialContact.getId())) {
            throw new BussinessException( BizExceptionEnum.REQUEST_NULL);
        }
        if("qq".equals(socialContact.getType())) {
            if(!RegExpValidatorUtil.isQq(socialContact.getAccount())) {
                throw new BussinessException(BizExceptionEnum.QQ_INVALID);
            }
        }
        socialContactMapper.updateById(socialContact);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除
     * @param socialId
     * @return
     */
    @RequestMapping(value = "/delete")
    @Permission
    @ResponseBody
    public Object delete(@RequestParam(value = "socialId") Integer socialId) {
        socialContactMapper.deleteById(socialId);
        return super.SUCCESS_TIP;
    }
}
